package cn.iocoder.yudao.module.erp.controller.admin.dpitem.dpitemdn;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.erp.controller.admin.dpitem.dpitemdn.vo.DpItemDnPageReqVO;
import cn.iocoder.yudao.module.erp.controller.admin.dpitem.dpitemdn.vo.DpItemDnRespVO;
import cn.iocoder.yudao.module.erp.controller.admin.dpitem.dpitemdn.vo.DpItemDnSaveReqVO;
import cn.iocoder.yudao.module.erp.dal.dataobject.dpitem.DpItemDnDO;
import cn.iocoder.yudao.module.erp.service.dpitem.DpItemDnService;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.*;
import javax.servlet.http.*;
import java.io.IOException;
import java.util.List;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;


@Tag(name = "管理后台 - ERP东宁制单货号详情")
@RestController
@RequestMapping("/erp/dp-item-dn")
@Validated
public class DpItemDnController {

    @Resource
    private DpItemDnService dpItemDnService;

    @PostMapping("/create")
    @Operation(summary = "创建ERP东宁制单货号详情")
    // @PreAuthorize("@ss.hasPermission('erp:dp-item-dn:create')")
    public CommonResult<Long> createDpItemDn(@Valid @RequestBody DpItemDnSaveReqVO createReqVO) {
        return success(dpItemDnService.createDpItemDn(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新ERP东宁制单货号详情")
    // @PreAuthorize("@ss.hasPermission('erp:dp-item-dn:update')")
    public CommonResult<Boolean> updateDpItemDn(@Valid @RequestBody DpItemDnSaveReqVO updateReqVO) {
        dpItemDnService.updateDpItemDn(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除ERP东宁制单货号详情")
    @Parameter(name = "id", description = "编号", required = true)
    // @PreAuthorize("@ss.hasPermission('erp:dp-item-dn:delete')")
    public CommonResult<Boolean> deleteDpItemDn(@RequestParam("id") Long id) {
        dpItemDnService.deleteDpItemDn(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得ERP东宁制单货号详情")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    // @PreAuthorize("@ss.hasPermission('erp:dp-item-dn:query')")
    public CommonResult<DpItemDnRespVO> getDpItemDn(@RequestParam("id") Long id) {
        DpItemDnDO dpItemDn = dpItemDnService.getDpItemDn(id);
        return success(BeanUtils.toBean(dpItemDn, DpItemDnRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得ERP东宁制单货号详情分页")
    // @PreAuthorize("@ss.hasPermission('erp:dp-item-dn:query')")
    public CommonResult<PageResult<DpItemDnRespVO>> getDpItemDnPage(@Valid DpItemDnPageReqVO pageReqVO) {
        PageResult<DpItemDnDO> pageResult = dpItemDnService.getDpItemDnPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, DpItemDnRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出ERP东宁制单货号详情 Excel")
    // @PreAuthorize("@ss.hasPermission('erp:dp-item-dn:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportDpItemDnExcel(@Valid DpItemDnPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<DpItemDnDO> list = dpItemDnService.getDpItemDnPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "ERP东宁制单货号详情.xls", "数据", DpItemDnRespVO.class,
                        BeanUtils.toBean(list, DpItemDnRespVO.class));
    }

}